<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class Unsubscribe {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("VIEW");
	}

	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		global $CONFIG;

		if (!$_GET['code'] or !$page->id)
			errorpage(9);

		$sqlresult = db_do_query($page, db_query_select(array(array('*')), array("MEMBER"), ereg("^[0-9]+$", $page->id) ? "user_id = {$page->id}" : "nick = '{$page->id}'"));
		
		if (db_num_rows($sqlresult) != 1)
			errorpage(9);
			
		$user = db_fetch_array($sqlresult);
		
		if ($_GET['code'] == (md5("{$user['nick']}{$user['email']}UNSUBSCRIBE"))) {
		
			db_do_query($page, db_query_update(array('member_since', 'last_visit', 'last_click', 'pref_newsletter'), array('member_since', 'last_visit', 'last_click', 0), "MEMBER", "user_id = {$user['user_id']}"));
			
			endpage("OK, '{$user['email']}' ontvang niet langer de mandragon nieuwsbrief. <script language=JavaScript>setTimeout(\"location.href='{$CONFIG['homedir']}'\", 5000);</script>");
		
		} else
			errorpage(14);
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress() {}
	//TODO: can this function be removed?
}

?>